<?php
/*
 +----------------------------------------------------------------------
 + Title        : IndexController 控制器
 + Author       : Randy_chen
 + Version      : V1.0.0.1
 + Initial-Time : 2022/3/15 21:14
 + Last-time    : 2022/3/15 21:14+ chen2
 + Desc         : IndexController
 +----------------------------------------------------------------------
*/


namespace app\api\controller\user;

use app\api\controller\ApiBaseController;
use app\common\enum\EAppointmentOrderStatus;
use app\common\model\AppointmentOrder;
use app\common\model\User;
use app\common\model\UserCard;
use app\common\model\UserServicePackage;
use app\common\model\UserServicePackageItem;
use randy\MyQuery;

class IndexController extends ApiBaseController
{
    public function getUserInfo()
    {
        $user = User::build()
                    ->with(['tjr'])
                    ->find(get_client_id());
        return $this->returnSuccess($user);
    }

    public function getOrderTotal()
    {
        // 订单状态 0=待确认 1=待服务 2=服务中 3=待核销 4=已完成 -1=已取消
        $user_id = get_client_id();
        $data['wait_confirm_num'] = AppointmentOrder::build()
                                                    ->where('user_id', $user_id)
                                                    ->where('order_status', 0)
                                                    ->count();
        $data['wait_service_num'] = AppointmentOrder::build()
                                                    ->where('user_id', $user_id)
                                                    ->where('order_status', 1)
                                                    ->count();
        $data['finish_num'] = AppointmentOrder::build()
                                              ->where('user_id', $user_id)
                                              ->where('order_status', 4)
                                              ->count();
        return $this->returnSuccess($data);
    }

    public function getCardTotal()
    {
        $user_id = get_client_id();
        $data['card_num'] = UserCard::build()
                                    ->where('user_id', $user_id)
                                    ->value('balance') ?? 0;
        $data['package_num'] = UserServicePackage::build()
                                                 ->whereIn('id', function (MyQuery $query) use ($user_id)
                                                 {
                                                     $query->name('user_package_relation')
                                                           ->where('user_id', $user_id)
                                                           ->field('user_service_package_id');
                                                 })
                                                 ->where('status', 1)
                                                 ->count();
        return $this->returnSuccess($data);
    }
}